/*
Pic Resizer : an easy to use program to resize your pictures
Copyright (C) 2009  Kévin Gomez <geek63@gmail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ResizeDial.h"

ResizeDial::ResizeDial(QImage *img_org, QImage *image, QWidget *parent) : QDialog(parent)
{
    img = image;
    org_img = img_org;

    setModal(true);
    setWindowTitle(tr("Redimensionnement personnalisé"));

    //création des widget
    hauteur_w = new QSpinBox;
    largeur_w = new QSpinBox;
    save_proportions_w = new QCheckBox(this);

    QPushButton *ok_w = new QPushButton(tr("Redimensionner"), this);
    QPushButton *cancel_w = new QPushButton(tr("Annuler"), this);

    // "config" des widget
    largeur_w->setRange(1, 4000);
    largeur_w->setValue(img->width());
    largeur_w->setSuffix(tr(" px"));

    hauteur_w->setRange(1, 4000);
    hauteur_w->setValue(img->height());
    hauteur_w->setSuffix(tr(" px"));

    save_proportions_w->setChecked(true);

    //construction du layout
    QHBoxLayout *buttons_l = new QHBoxLayout;
    buttons_l->addWidget(ok_w);
    buttons_l->addWidget(cancel_w);

    QFormLayout *form_l = new QFormLayout;
    form_l->addRow(tr("&Largeur :"), largeur_w);
    form_l->addRow(tr("&Hauteur :"), hauteur_w);
    form_l->addRow(tr("Conserver les &proportions :"), save_proportions_w);
    form_l->addRow(buttons_l);
    setLayout(form_l);

    //connexions aux évènements
    connect(ok_w, SIGNAL(clicked()), this, SLOT(resize()));
    connect(cancel_w, SIGNAL(clicked()), this, SLOT(reject()));
}

void ResizeDial::resize()
{
    if(!save_proportions_w->isChecked())
        *img = org_img->scaled(largeur_w->value(), hauteur_w->value());
    else
    {
        *img = (largeur_w->value() >= hauteur_w->value())
                ? org_img->scaledToWidth(largeur_w->value(), Qt::SmoothTransformation)
                : org_img->scaledToHeight(hauteur_w->value(), Qt::SmoothTransformation);
    }

    emit resized();
    accept();
}

void ResizeDial::run()
{
    show();
    exec();
}
